// pages/orders/index/index.js
import http from '../../../services/http/index'
import { ListData } from '../../../utils/Data'
let util = require('../../../utils/index')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cindex: 0,//当前接单类型，0:结伴班车,1:机场接送,2:预约用车,3:包车旅游,
    orderId: 0,//订单id
    ordersNavs: ['结伴班车', '机场接送', '预约用车', '包车旅游'],
    itemList: ['上班小分队', 'kevin小班车', 'Skycar'],
    airItemList: ['接机', '送机', '往返'],

    airItemImgUrl: ['../../../images/icon_pickup@2x.png', '../../../images/icon_send@2x.png', '../../../images/icon_goandback@2x.png'],
    itemBusListicon_next: {}, //"班车"抢单列表
    itemMissionList: {}, //"机场接送"抢单列表
    itemAppointCar: {},//“预约用车”抢单列表
    itemCharter: {},//“包车”抢单列表

  },
  // tab选择
  chooseType(e) {
    let type = e.currentTarget.dataset['type'];
    this.setData({
      cindex: type
    })
    console.log(type);
    if (type == 0){
      // 刷新"班车"抢单列表
      this.getBusInfo();
    }else if(type == 1){
      // 刷新"机场接送"抢单列表
      this.getMissionInfo();
    }else if (type == 2) {
      // 刷新"预约用车"抢单列表
      this.getAppointCarInfo();
    }else if (type == 3) {
      // 刷新"包车"抢单列表
      this.getCharterInfo();
    }
  },
  // "机场接送"“预约用车”
  next(e) {
    // 获取接单id-跳转对应详情页
    var missProId = e.currentTarget.dataset.id
    util.default.go(`../detail/detail_1?type=${this.data.cindex}&orderId=${missProId}`)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: 'Skycar-车来了'
    })
    // 刷新"班车"抢单列表
    this.getBusInfo();
  },


  // 请求-"班车"抢单列表
  getBusInfo() {
    var that = this;
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000
    });
    http.getBus()
      .then(data => {
        var itemBusListData = Object.assign(new ListData(), {
          data: data.list
        })
        that.setData({
          itemBusList: itemBusListData.data
        });
        wx.hideToast();
      }).catch(err => {
        wx.hideToast();
        console.log(err)
      })
  },
  // 请求-"机场接送"抢单列表
  getMissionInfo() {
    var that = this;
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000,
      mask:true
    });
    http.getMission()
      .then(data => {
        var itemMissionListData = Object.assign(new ListData(), {
          data: data.mission
        })
        that.setData({
          itemMissionList: itemMissionListData.data
        });
        wx.hideToast();
      }).catch(err => {
        wx.hideToast();
        console.log(err)
      })
  },
  // 请求-"预约用车"抢单列表
  getAppointCarInfo() {
    var that = this;
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000
    });
    http.getAppointCar()
      .then(data => {
        console.log('预约用车------------');
        console.log(data);
        console.log('预约用车-----------');
        var itemAppointCarData = Object.assign(new ListData(), {
          data: data.list
        })
        that.setData({
          itemAppointCar: itemAppointCarData.data
        });
        wx.hideToast();
      }).catch(err => {
        wx.hideToast();
        console.log(err)
      })
  },

  // 请求-"包车"抢单列表
  getCharterInfo() {
    var that = this;
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000
    });
    http.getCharter()
      .then(data => {
        console.log('包车------------');
        console.log(data);
        console.log('包车-----------');
        var itemCharterData = Object.assign(new ListData(), {
          data: data.list
        })
        that.setData({
          itemCharter: itemCharterData.data
        });
        wx.hideToast();
      }).catch(err => {
        wx.hideToast();
        console.log(err)
      })
  },




  // 抢单成功
  robbingSucces() {
    wx.showToast({
      title: '抢单成功',
      icon: 'succes',
      duration: 5000,
      success: function () {
        setTimeout(function () {
          //要延时执行的代码
          wx.switchTab({
            url: '../../orderManage/orderManage'
          })
        }, 2000) //延迟时间
      }
    })
  },

  // 请求-抢单 班车
  RobbingBus(e) {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000
    });
    var that = this;
    var idRobbing = e.currentTarget.dataset.id;
    console.log("idRobbing是" + idRobbing)
    http.getRobbingBus({ id: idRobbing }).then(data => {
      console.log(data)
      wx.hideToast();
      // 刷新"班车"抢单列表
      that.getBusInfo();
      // "班车"抢单成功
      that.robbingSucces();
    }).catch(function (err) {
      console.log(err)
      wx.hideToast();
      if (err.payload.status == 2) {
        wx.showModal({
          title: '',
          content: err.message,
          success: function (res) {
            if (res.confirm) {
              http.getRobbingBus({ id: idRobbing, is_confirm: 1 }).then(data => {
                // 刷新"班车"抢单列表
                that.getBusInfo();
                // "班车"抢单成功
                that.robbingSucces();
                wx.hideToast();
              }).catch(function (err) {
                wx.hideToast();
              })
            } else {
              console.log('用户点击取消')
            }
          }
        })
      } else {
        wx.showModal({
          title: '',
          content: err.message,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              // 刷新"班车"抢单列表
              that.getBusInfo();
              console.log('用户点击确定')
            } else {
              console.log('用户点击取消')
            }
          }
        })
      }

    })
  },
  // 请求-抢单 接机单
  RobbingMission(e) {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000,
      mask:true
    });
    var that = this;
    var idRobbing = e.currentTarget.dataset.id;
    console.log("idRobbing是" + idRobbing)
    http.getRobbingMission({ id: idRobbing }).then(data => {
      console.log(data)
      wx.hideToast();
      // 刷新"机场接送"抢单列表
      that.getMissionInfo();
      // "机场接送"抢单成功
      that.robbingSucces();
    }).catch(function (err) {
      console.log(err)
      wx.hideToast();
      if (err.payload.status == 2) {
        wx.showModal({
          title: '',
          content: err.message,
          success: function (res) {
            if (res.confirm) {
              http.getRobbingMission({ id: idRobbing, is_confirm: 1 }).then(data => {
                // 刷新"机场接送"抢单列表
                that.getMissionInfo();
                // "机场接送"抢单成功
                that.robbingSucces();
              }).catch(function (err) {
                wx.hideToast();
              })
            } else {
              console.log('用户点击取消')
            }
          }
        })
      } else {
        wx.showModal({
          title: '',
          content: err.message,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              // 刷新"机场接送"抢单列表
              that.getMissionInfo();
              console.log('用户点击确定')
            } else {
              console.log('用户点击取消')
            }
          }
        })
      }

    })
  },
  // 请求-抢单 预约用车
  CarMission(e) {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000,
      mask:true
    });
    var that = this;
    var idRobbing = e.currentTarget.dataset.id;
    console.log("idRobbing是" + idRobbing)
    http.appointCarGrap({ id: idRobbing }).then(data => {
      console.log(data)
      wx.hideToast();
      // 刷新"预约用车"抢单列表
      that.getAppointCarInfo();
      // "预约用车"抢单成功
      that.robbingSucces();
    }).catch(function (err) {
      console.log(err)
      wx.hideToast();
      if (err.payload.status == 2) {
        wx.showModal({
          title: '',
          content: err.message,
          success: function (res) {
            if (res.confirm) {
              http.appointCarGrap({ id: idRobbing, is_confirm: 1 }).then(data => {
                // 刷新"预约用车"抢单列表
                that.getAppointCarInfo();
                // "预约用车"抢单成功
                that.robbingSucces();
              }).catch(function (err) {
                wx.hideToast();
              })
            } else {
              console.log('用户点击取消')
            }
          }
        })
      } else {
        wx.showModal({
          title: '',
          content: err.message,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              // 刷新"预约用车"抢单列表
              that.getAppointCarInfo();
              console.log('用户点击确定')
            } else {
              console.log('用户点击取消')
            }
          }
        })
      }
    })
  },

  // 请求-抢单 包车
  Robbingcharter(e) {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000,
      mask:true
    });
    var that = this;
    var idRobbing = e.currentTarget.dataset.id;
    console.log("idRobbing是" + idRobbing)
    http.getRobbingcharter({ id: idRobbing }).then(data => {
      console.log(data)
      wx.hideToast();
      // 刷新"包车"抢单列表
      that.getCharterInfo();
      // "包车"抢单成功
      that.robbingSucces();
    }).catch(function (err) {
      console.log(err)
      wx.hideToast();
      if (err.payload.status == 2) {
        wx.showModal({
          title: '',
          content: err.message,
          success: function (res) {
            if (res.confirm) {
              http.getRobbingcharter({ id: idRobbing, is_confirm: 1 }).then(data => {
                // 刷新"包车"抢单列表
                that.getCharterInfo();
                // "包车"抢单成功
                that.robbingSucces();
              }).catch(function (err) {
                wx.hideToast();
              })
            } else {
              console.log('用户点击取消')
            }
          }
        })
      } else {
        wx.showModal({
          title: '',
          content: err.message,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              // 刷新"包车"抢单列表
              that.getCharterInfo();
              console.log('用户点击确定')
            } else {
              console.log('用户点击取消')
            }
          }
        })
      }

    })
  },





})